#!/usr/bin/env python
# coding=utf-8
#--------------------------------------------------------------------------
# 文件名:       Translator.py 
# 说明：        目的、作用说明
#
# 版本：        0.01
# 作者：        xiao
#
# 创建时间：    2011-11-19 
# 许可证：      Apache License V2.0
#--------------------------------------------------------------------------

import CollectBase as cb
from pyquery import PyQuery as pq
import os
import time
import random
import sqlite3DB
import XiaoTool as xt

## bing的翻译接口
#
class TranslatorCall(cb.CollectBase):

	## 方法注释
	#
	#  @param
	#  @return
	#
	def parseData(self, txt, resp, urlDict):
		if resp["status"] == "403":
			time.sleep(random.randint(3000, 3500))
			return urlDict

		"""
		if os.path.isfile("num_q"):
			num_q = int(open("num_q").read().strip()) + 1

			if num_q > random.randint(20, 30):
				time.sleep(random.randint(30, 80))
				num_q = 1

			open("num_q", "w").write(str(num_q))
		else:
			open("num_q", "w").write("1")
		"""
		d = pq(txt)
		db = sqlite3DB.DbAction("d_ea.db")
		db.dbLoad()

		for dt in d(".result-title a"):
			try:
				db.setExec((u"INSERT INTO `product`(`name`, `description`, `reurl`, `flag`) VALUES ('', '', 'http://ezinearticles.com%s', 0);" % xt.html_clear_tag(pq(dt).attr("href"))))
			except:
				print(urlDict['url'])

		db.actionExec()
		db.dbClose()

		nextUrl = d(".next").attr("href")
		if nextUrl is not None:
			outDict = {"url": "http://ezinearticles.com%s" % nextUrl}
			open("last_url.txt", "w").write(nextUrl)
			return outDict
	#------------------------------------------ End def parseData()
#------------------------------------------------------ End class Translator()

if __name__ == "__main__":
	TranslatorCall([{"url":"http://ezinearticles.com/search/?q=dvd"}])
